package com.sunday.haowushop.wxapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


import com.sunday.common.utils.ToastUtils;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.common.Constant;
import com.sunday.haowushop.entity.Order;
import com.sunday.haowushop.ui.order.GroupBuyOrderListActivity;
import com.sunday.haowushop.ui.order.OrderListActivity;
import com.sunday.haowushop.ui.order.PaySuccessActivity;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.math.RoundingMode;


/**
 * Created by 刘涛 on 2017/4/28.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transparent);

        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        Intent intent = null;
        switch (resp.errCode) {
            case 0:
                int orderType = BaseApp.getInstance().getPayType();
//                intent = new Intent(WXPayEntryActivity.this, PaySuccessActivity.class);//支付成功界面
//                Order order = BaseApp.getInstance().getOrder();
//                intent.putExtra("totalMoney", String.format("%s", order.getTotalMoney().setScale(2, RoundingMode.HALF_UP)));
//                intent.putExtra("address", order.getAddress());
//                intent.putExtra("name", order.getName());
//                intent.putExtra("mobile", order.getMobile());
//                startActivity(intent);
                ToastUtils.showToast(WXPayEntryActivity.this, "交易成功");
                intent = new Intent(WXPayEntryActivity.this, OrderListActivity.class);
                intent.putExtra("page", 2);
                startActivity(intent);
                finish();
                break;
            case -1:
                ToastUtils.showToast(WXPayEntryActivity.this, "交易失败,请重试");
                intent = new Intent(WXPayEntryActivity.this, OrderListActivity.class);
                intent.putExtra("page", 1);
                startActivity(intent);
                finish();
                break;
            case -2:
                ToastUtils.showToast(WXPayEntryActivity.this, "交易取消");
                intent = new Intent(WXPayEntryActivity.this, OrderListActivity.class);
                intent.putExtra("page", 1);
                startActivity(intent);
                finish();
                break;
        }


    }
}